package xyz.yuzegod.megawalls.classes.normal.arcane;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.PlayerUtils;
import xyz.yuzegod.megawalls.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class ThirdSkill extends Skill
{
    public ThirdSkill(final Classes classes) {
        super("奥术爆炸", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 3;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 0.14;
            }
            case 2: {
                return 0.17;
            }
            case 3: {
                return 0.2;
            }
            default: {
                return 0.14;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7被攻击时,有§a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率发动奥术爆炸");
            lore.add("   §7对你周围的敌人造成1次伤害");
            return lore;
        }
        lore.add(" §8▪ §7被攻击时,有§8" + StringUtils.percent(this.getAttribute(level - 1)) + " ➜ §a" + StringUtils.percent(this.getAttribute(level)) + "§7的几率发动奥术爆炸");
        lore.add("   §7对你周围的敌人造成1次伤害");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkill3Level();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkill3Level();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        if (MegaWalls.getRandom().nextInt(100) <= this.getAttribute(kitStats.getSkill3Level()) * 100.0) {
            gamePlayer.getPlayer().getWorld().createExplosion(gamePlayer.getPlayer().getLocation(), 0.0f);
            for (final Player player : this.getNearbyPlayers(gamePlayer.getPlayer(), 5)) {
                player.damage(1.0, (Entity)gamePlayer.getPlayer());
            }
        }
        return true;
    }
    
    private List<Player> getNearbyPlayers(final Player player, final int radius) {
        final List<Player> players = new ArrayList<Player>();
        for (final Player other : PlayerUtils.getNearbyPlayers((Entity)player, radius)) {
            final GamePlayer gameOther = GamePlayer.get(other.getUniqueId());
            if (!gameOther.isSpectator()) {
                if (GamePlayer.get(player.getUniqueId()).getGameTeam().isInTeam(gameOther)) {
                    continue;
                }
                players.add(other);
            }
        }
        return players;
    }
}
